function zbar = AES2019P2(z,alphac,alphad,alphax,alphasigma,gamma,gammatilda,rho,mu,nux,nusigma,sigmabar,psic,psid,chi,A1v,A2v_2)
%AES2019P2 Function that is used to solve the fixed point in the Long-run
%Risk Model (Andreis, Eisenbach, and Schmalz 2023)
%   Input: z is the log price-dividend ratio, whose equilibrium value is
%   what we want to solve; all other inputs are the same as the file
%   AES2023 appendix and are given.

% kappa for wealth
pibar = -mu-0.5*(1-rho)*((1-gamma)+nusigma*(gamma-gammatilda))*(alphac^2+A1v^2*alphax^2)*sigmabar^2 ...
-0.5*(1-gamma)^2*A2v_2^2*(1-gammatilda)^2*alphasigma^2; % equilibrium log SDF
kappa1 = exp(z)/(1+exp(z)); % kappa from CS1988
kappa0 = log(1+exp(z))-kappa1*z;

% Coefficients of z
A1 = (psid-rho*psic)/(1-kappa1*nux); % as defined in AES2023 appendix
A2 = (-0.5*((rho-gamma)*(1-gamma)-(1-rho)*(gamma-gammatilda)*nusigma)*(alphac^2+A1v^2*alphax^2)...
    +0.5*(chi-gamma)^2*alphac^2+0.5*(kappa1*A1+(rho-gamma)*A1v)^2*alphax^2 ...
    +0.5*alphad^2)/(1-kappa1*nusigma);
A0 = (1/(1-kappa1))*(pibar+kappa0+kappa1*A2*sigmabar^2*(1-nusigma)...
    +0.5*(((rho-gamma)*(1-gammatilda)+(1-rho)*(gamma-gammatilda)*(1-nusigma))*A2v_2+kappa1*A2)^2*alphasigma^2);

% Equilibrium values
zbar = A0+A2*sigmabar^2-z; % equilibrium condition is "z==zbar"

% End of function
end

